package top.fyxxxx.dbdesign.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.dom4j.Element;
import top.fyxxxx.dbdesign.util.ConfigProvider;

public class Connect {
    private static Connection conn = null;


    public static Connection getConn(){
        try {
            if(conn==null||conn.isClosed()){
                synchronized (Connect.class){
                    if(conn==null||conn.isClosed()){
                        conn=createConnection();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    private static Connection createConnection() throws Exception {
        String DB_URL,USER,PASS;
        Element sql= ConfigProvider.getConfig("SQL");
        // 注册 JDBC 驱动
        Class.forName(sql.elementText("JDBC_DRIVER"));
        DB_URL=sql.elementText("url");
        USER=sql.elementText("username");
        PASS=sql.elementText("password");
        return DriverManager.getConnection(DB_URL,USER,PASS);
    }
    static int init()
    {
        int tabels_num=5;
        Connection conn = Connect.getConn();
        String sql[]=new String[5];
        sql[0]= "CREATE TABLE IF NOT EXISTS user(\n" +
                "   account VARCHAR(255) NOT NULL unique,\n" +
                "   password VARCHAR(255) NOT NULL,\n" +
                "   uname VARCHAR(255),\n" +
                "   uid BIGINT NOT NULL,\n" +
                "   rdate timestamp NOT NULL,\n"+
                "   power BIGINT NOT NULL,\n" +
                "   PRIMARY KEY ( uid ),\n" +
                "   KEY( account )\n" +
                ")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        sql[1]="CREATE TABLE IF NOT EXISTS course(\n" +
                "   cname VARCHAR(255) NOT NULL,\n" +
                "   cid BIGINT NOT NULL,\n" +
                "   rdate timestamp NOT NULL,\n"+
                "   uid BIGINT NOT NULL,\n" +
                "   PRIMARY KEY ( cid ),\n" +
                "   INDEX( uid )\n" +
                ")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        sql[2]="CREATE TABLE IF NOT EXISTS uc(\n" +
                "   uid BIGINT NOT NULL,\n" +
                "   cid BIGINT NOT NULL,\n" +
                "   rdate timestamp NOT NULL,\n"+
                "   power BIGINT NOT NULL,\n" +
                "   PRIMARY KEY ( uid,cid ),\n" +
                "   INDEX( cid ),\n" +
                "   INDEX( uid )\n" +
                ")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        sql[3]="CREATE TABLE IF NOT EXISTS signInTask(\n" +
                "   uid BIGINT NOT NULL,\n" +
                "   cid BIGINT NOT NULL,\n" +
                "   sid BIGINT NOT NULL,\n" +
                "   rdate timestamp NOT NULL,\n"+
                "   beginTime timestamp NOT NULL,\n"+
                "   endTime timestamp NOT NULL,\n"+
                "   PRIMARY KEY ( sid,cid ),\n" +
                "   INDEX( cid ),\n" +
                "   INDEX( uid )\n" +
                ")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        sql[4]="CREATE TABLE IF NOT EXISTS signIn(\n" +
                "   uid BIGINT NOT NULL,\n" +
                "   cid BIGINT NOT NULL,\n" +
                "   sid BIGINT NOT NULL,\n" +
                "   sidate timestamp NOT NULL,\n"+
                "   PRIMARY KEY ( uid,sid,cid ),\n" +
                "   INDEX( cid ),\n" +
                "   INDEX( uid ) \n" +
                ")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        int ans=0;
        for(int i=0;i<tabels_num;i++)
        {
            if(exeSQL(sql[i],conn)>0)
            {
                ans<<=1;
                ans+=1;
            }
        }
        return ans;
    }
    private static int exeSQL(String sql, Connection conn)
    {
        int i = -1;
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
}
